package com.qf.web;

import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.service.ProductService;
import com.qf.service.serviceimpl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/product")
public class ProductServlet extends BaseServlet {
    /**
     *通过pid查找商品
     */
    public void findProductBypid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        ProductService ps = new ProductServiceImpl();
        Product product = ps.findProductByPid(pid);
        //System.out.println(product);
        request.setAttribute("product",product);
        request.getRequestDispatcher("/jsp/product_info.jsp").forward(request,response);
    }
    /**
     * 通过cid查找商品,完成分页操作
     */
    public void findProductByCid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cP = request.getParameter("currentPage");
        String pS = request.getParameter("pageSize");
        String cid = request.getParameter("cid");
        int currentPage = Integer.parseInt(cP);
        int pageSize = Integer.parseInt(pS);
        //System.out.println(currentPage+":"+pageSize+":"+cid);
        ProductService ps = new ProductServiceImpl();
        PageBean<Product> products = ps.findProducts(currentPage,pageSize,cid);
        System.out.println(products);
        request.setAttribute("products",products);
        request.getRequestDispatcher("/jsp/product_list.jsp").forward(request,response);
    }
}
